﻿using System;
using System.Data;
using System.Collections.Generic;

namespace JWFW.Model.Register.BRL
{
#if Server
	public partial class Account : BOL.BaseEntity
	{
        /// <summary>
        /// 帐户信息类
        /// </summary>
		public Account()
		{
			m_tableName = "rem_account";
			m_hasName = true;
            m_hasComment = true;
			m_objCacheManager.Size = 300;
		}

		protected int m_toolID = 0;
		protected string m_machineCode = string.Empty;
		protected string m_registerCode = string.Empty;
		protected string m_specialInfo = string.Empty;
        protected DateTime m_expireDate = DateTime.Now;
        protected bool m_isEnterprise = false;
        protected string m_mcRanger = "";
        protected int m_number = 1;
        protected string m_version = "";
        protected Tool m_tool = null;
        


		/// <summary>
		/// 工具ID
		/// </summary>
		public int ToolID
		{
			get { return m_toolID; }
			set { m_toolID = value; }
		}
		/// <summary>
		/// 机器标识码
		/// </summary>
		public string MachineCode
		{
			get { return m_machineCode; }
			set { m_machineCode = value; }
		}
		/// <summary>
		/// 注册码
		/// </summary>
		public string RegisterCode
		{
			get { return m_registerCode; }
			set { m_registerCode = value; }
		}
		/// <summary>
		/// 额外信息
		/// </summary>
		public string SpecialInfo
		{
			get { return m_specialInfo; }
			set { m_specialInfo = value; }
		}
		/// <summary>
		/// 有效期
		/// </summary>
		public DateTime ExpireDate
		{
			get { return m_expireDate; }
			set { m_expireDate = value; }
		}

        /// <summary>
        /// 是否是企业版
        /// </summary>
        public bool IsEnterprise
        {
            get { return m_isEnterprise; }
            set { m_isEnterprise = value; }
        }
        /// <summary>
        /// 企业版使用的机器码范围，使用;分割，最后一个必须有;
        /// </summary>
        public string MCRanger
        {
            get { return m_mcRanger; }
            set { m_mcRanger = value; }
        }
        /// <summary>
        /// 此帐号中允许的数量
        /// </summary>
        public int Number
        {
            get { return m_number; }
            set { m_number = value; }
        }
        /// <summary>
        /// 当前客户端的版本，不保存于数据库，用于存放解析的客户提交信息
        /// </summary>
        public string Version
        {
            get { return m_version; }
            set { m_version = value; }
        }
        /// <summary>
        /// 本帐户对应的工具
        /// </summary>
        public Tool Tool
        {
            get
            {
                if (m_tool == null || m_tool.ID != m_toolID)
                {
                    m_tool = new Tool();
                    m_tool.GetProperties(m_toolID);
                }
                return m_tool;
            }
        }

		protected override void FillContainer()
		{
			base.FillContainer();
			m_container.Add("toolID", m_toolID);
			m_container.Add("machineCode", m_machineCode);
			m_container.Add("registerCode", m_registerCode);
			m_container.Add("specialInfo", m_specialInfo);
            m_container.Add("expireDate", m_expireDate);
            m_container.Add("isEnterprise", m_isEnterprise);
            m_container.Add("mcRanger", m_mcRanger);
            m_container.Add("number", m_number);
		}

		public override bool GetProperties(System.Data.DataRow record)
		{
			if(!base.GetProperties(record))
			{
				return false;
			}
			m_toolID = this.GetIntValue(record, "toolID");
			m_machineCode = this.GetStringValue(record, "machineCode");
			m_registerCode = this.GetStringValue(record, "registerCode");
			m_specialInfo = this.GetStringValue(record, "specialInfo");
			m_expireDate = this.GetDateValue(record, "expireDate");
            m_isEnterprise = this.GetBoolValue(record, "isEnterprise");
            m_mcRanger = this.GetStringValue(record, "mcRanger");
            m_number = this.GetIntValue(record, "number");
			return true;
		}

        /// <summary>
        /// 是否存在指定的帐户
        /// </summary>
        /// <returns></returns>
        public bool ExistSpecialAccount()
        {
            string cond = string.Format("Where {0}={1} and {2}={3} and {4}={5} and {6}<>{7} ",
                DB.GetCol("name"), DB.GetVal(m_name),
                DB.GetCol("toolId"), DB.GetVal(m_toolID),
                DB.GetCol("specialInfo"), DB.GetVal(m_specialInfo),
                DB.GetCol("id"), DB.GetVal(m_id)
                );
            //string.Format(" WHERE [name]={0}' AND [toolId]={1} AND [specialInfo]={2}' AND [id] <> {3}", m_name, m_toolID, m_specialInfo, m_id);
            return this.Exist("", cond, false);
        }
        /// <summary>
        /// 获取实例数据
        /// </summary>
        /// <param name="name">帐户名</param>
        /// <param name="toolId">工具ID</param>
        /// <param name="specialInfo">额外的信息</param>
        public void GetProperties(string name, int toolId, string specialInfo)
        {
            //string cond = string.Format(" WHERE [name]=N'{0}' AND [toolId]={1} AND [specialInfo]=N'{2}'", name, toolId, specialInfo);
            string cond = string.Format(" Where {0}={1} and {2}={3} and {4}={5}",
                DB.GetCol("name"), DB.GetVal(name),
                DB.GetCol("toolId"), DB.GetVal(toolId),
                DB.GetCol("specialInfo"), DB.GetVal(specialInfo));
            DataTable dt = this.GetRecords(cond);
            if (dt.Rows.Count > 0)
            {
                this.GetProperties(dt.Rows[0]);
            }
        }
	}
#endif
}

